package liuhaoran;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class DoubleColorBall {


    public static void main(String[] args) {
        int[] winningnumbers = RandomWinningNumber();//随机生成中奖号码
        int[] Inputnumber = InputNumber();
        cheekResult(winningnumbers,Inputnumber);
    }

    private static void cheekResult(int[] winningnumbers,int[] user) {
            int[] winRed = Arrays.copyOf(winningnumbers,6);
            int[] userRed = Arrays.copyOf(user,6);
            int winBule = winningnumbers[6];
            int userBule = user[6];

            int redMatch = 0;
        Set<Integer>winRedSet = new HashSet<>();
        for (int num : winRed)winRedSet.add(num);
        for (int num : userRed){
            if(winRedSet.contains(num))redMatch++;
        }
        boolean buleMatch = (winBule == userBule);
        System.out.println("\n红球命中数：" +redMatch);
        System.out.println("篮球"+(buleMatch?"命中":"未命中"));
        String prizeLevel = "未中奖";
        if(redMatch == 6 && buleMatch)
        {
            prizeLevel = "一等奖";
        } else if (redMatch==6) {
            prizeLevel = "二等奖";
        } else if (redMatch==5 && buleMatch) {
            prizeLevel = "三等奖";
        }else if ((redMatch==4 && buleMatch)||(redMatch==5)) {
            prizeLevel = "四等奖";
        }else if ((redMatch==3 && buleMatch)||(redMatch==4)) {
            prizeLevel = "五等奖";
        }else if ((redMatch==2 && buleMatch)||(redMatch==1 && buleMatch) || (buleMatch)) {
            prizeLevel = "六等奖";
        }
        System.out.println("中奖结果：" + prizeLevel);

    }

    //随机生成中奖号码
    private static int[] RandomWinningNumber(){
            int[] numbers = new int[7];
        for (int i = 0; i < 6; i++) {
            boolean a;
            do {
                a = true;
                numbers[i] = (int)(Math.random() * 33) + 1;
                for (int j = 0; j < i; j++) {
                    if (numbers[i] == numbers[j]){
                        a = false;
                        break;
                    }
                }
            }while (!a);
        }
        boolean b;
        do {
            b = true;
            numbers[6] = (int)(Math.random() * 16) + 1;
        }while (!b);
        System.out.println("中奖号码为：" + Arrays.toString(numbers));
        return numbers;
    }
    private static int[] InputNumber(){
        Scanner scanner = new Scanner(System.in);
        int[] numbers = new int[7];
        System.out.println("请输入6个红球号码，输入任意一个范围为1-33的数字：");
        for (int i = 0; i < 6; ) {
            System.out.println("请输入第"+(i+1)+"个红球");
            while (!scanner.hasNext()){
                System.out.println("输入错误，请重新输入任意一个范围为1-33的数字：");
                scanner.next();
                System.out.println("请输入第"+(i+1)+"个红球");
            }
            int input = scanner.nextInt();
            if (input<1||input>33){
                System.out.println("输入错误，请重新输入任意一个范围为1-33的数字：");
                continue;
            }
            boolean a = false;
            for (int j = 0; j < i; j++) {
                if (numbers[j]==input){
                    a = true;
                    break;
                }
            }
            if (a){
                System.out.println("输入错误，请重新输入任意一个范围为1-33的数字：");
            }else {
                numbers[i] = input;
                i++;
            }
        }
        System.out.println("请输入篮球号码，输入任意一个范围为1-16的数字：");
        while (true){
            while (!scanner.hasNext()){
                System.out.println("输入错误，请重新输入任意一个范围为1-16的数字：");
                scanner.next();
                System.out.println("请输入篮球号码，输入任意一个范围为1-16的数字：");
            }
            int Buleball = scanner.nextInt();
            if (Buleball >= 1 && Buleball<=16){
                numbers[6]=Buleball;
                break;
            }
            System.out.println("输入超出范围，输入任意一个范围为1-16的数字：");
            System.out.println("请输入篮球号码，输入任意一个范围为1-16的数字：");
        }
        System.out.println("\n你购买的的号：");
        System.out.println("红球"+Arrays.toString(Arrays.copyOfRange(numbers,0,6)));
        System.out.println("篮球" + numbers[6]);
        return numbers;
    }
    }


